<template>
	<view class="container">
		<view class="group-box">
			<view class="form-list">
				<view class="list-box">
					<text class="title">发票抬头</text>
					<view class="remark">
						<input class="input" type="text" v-model="model.title" placeholder="填写发票抬头" />
					</view>
				</view>
				<view class="list-box">
					<text class="title">税号</text>
					<view class="remark">
						<input class="input" type="number" v-model="model.dutyParagraph" placeholder="填写税号" />
					</view>
				</view>
				<view class="list-box">
					<text class="title">发票类型</text>
					<view class="remark">
						<uni-data-checkbox mode="tag" v-model="model.invoiceType"
							:localdata="invoiceTypeRange"></uni-data-checkbox>
					</view>
				</view>
			</view>
		</view>

		<view class="foot-wrap">
			<view class="foot-box">
				<text @click="submit" class="btn-add">确认提交</text>
			</view>
		</view>
	</view>
</template>

<script>
	 
	export default {
		 
		data() {
			return {
				invoiceTypeRange: [{
						text: '普通',
						value: 0
					},
					{
						text: '增值税',
						value: 1
					}
				], //发票类型
				model: {
					id: 0,
					userId: 0,
					title: '',
					dutyParagraph: '',
					invoiceType: 0,
					addTime: null
				}
			}
		},
		methods: {
			//初始化数据
			async initData(id) {
				//正在加载
				uni.showLoading({
					title: '加载中'
				});
				if (id) {
					await this.loadData(id);
				}				
				//关闭加载
				uni.hideLoading();
			},
			//加载地址信息
			async loadData(id) {
				let _this = this;
				await _this.$api.request({
					url: `/account/member/invoice/${id}`,
				}).then(res => {
					_this.model = res.data;					
				});
			},			 
			 
			//提交
			async submit() {
				let model = this.model;
				if (!model.title) {
					this.$api.msg('请填写发票抬头');
					return;
				}				 
				if (!model.dutyParagraph) {
					this.$api.msg('请填写税号');
					return;
				}
				
				if (model.id > 0) {
					await this.$api.request({
						url: `/account/member/invoice/${model.id}`,
						method: "put",
						data: model,
						loading: true
					}).then(res => {
						this.goBack(model);
					});
				} else {
					await this.$api.request({
						url: `/account/member/invoice`,
						method: "post",
						data: model,
						loading: true
					}).then(res => {
						this.goBack(model);
					});
				}
			},
			//返回并刷新上一页
			goBack(data) {
				//this.$api.prePage()获取上一页实例，可直接调用上页所有数据和方法
				this.$api.prePage().refreshList(data);
				this.$api.msg(`${data.id>0? '修改': '添加'}成功`, 1500, false, 'success');
				setTimeout(() => {
					uni.navigateBack();
				}, 800);
			}
		},
		//页面初始化
		onLoad(options) {
			//初始化数据
			this.initData(options.id);
		}
	}
</script>

<style lang="scss">
	.foot-wrap {
		height: 96rpx;

		.foot-box {
			display: flex;
			justify-content: space-between;
			align-items: center;
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			padding: 0 24rpx;
			height: 100rpx;

			.btn-add {
				display: block;
				padding: 0 48rpx;
				color: #fff;
				font-size: 30rpx;
				line-height: 76rpx;
				width: 100%;
				height: 76rpx;
				text-align: center;
				background: #ff5000;
				border-radius: 48rpx;
				box-shadow: 0 2rpx 8rpx rgba($color: #ff5000, $alpha: 0.2)
			}
		}
	}
</style>